import { _decorator, Component, Label, ProgressBar, Sprite, Animation, Node, SpriteFrame, find } from 'cc';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { Card, CardFrame, CardImage, CardPropertyAmplification, CardPropertyType, CARDTYPE, needCard, PROPERTY } from '../AshFramework/Datas/GameConstant';
import { BundleManager } from '../AshFramework/Managers/BundleManager';
import { GameManager } from './GameManager';
import { CardParticulars } from './GameUI/CardParticulars';
const { ccclass, property } = _decorator;

@ccclass('CardController')
export class CardController extends Component {

    @property(Sprite)
    FrameSprite: Sprite = null;

    @property(Sprite)
    ImageSprite: Sprite = null;

    @property(Label)
    GradeLabel: Label = null;

    @property(ProgressBar)
    UpgradeProgress: ProgressBar = null;

    @property(Node)
    UpgradeNode: Node = null;

    @property(Label)
    HaveLabel: Label = null;

    @property(Label)
    NeedLabel: Label = null;

    @property(Node)
    UpgradeAnimation: Node = null;

    private _type: CARDTYPE = CARDTYPE.CARD_GRAY;
    private _number: number = 0;
    private _grade: number = 1;
    private _haveCard: number = 0;
    private _needCard: number = 0;
    private _card: Card | null = null;
    private _isCard: boolean = true;
    private _amplificationType: string | null = null;
    private _amplification: number = 0;
    private _curAmplification: number = 0;

    clickBtn() {

        let node = find("Canvas/卡牌详情弹窗");
        if (node) {
            node.active = true;
            node.getComponent(CardParticulars).show(this._card);
        }
    }

    initCard(card: Card, isCard: boolean = true) {
        let frameSF: SpriteFrame = BundleManager.AssetMap.get(CardFrame[card.type]) as SpriteFrame;
        if (!frameSF) {
            console.error(`在分包内找不到${CardFrame[card.type]}资源！`);
        }
        this.FrameSprite.spriteFrame = frameSF;

        let imageSF: SpriteFrame = BundleManager.AssetMap.get(CardImage[card.type][card.number]) as SpriteFrame;
        if (!imageSF) {
            console.error(`在分包内找不到${CardImage[card.type][card.number]}资源！`);
        }
        this.ImageSprite.spriteFrame = imageSF;

        this._isCard = isCard;
        this._type = card.type;
        this._number = card.number;
        this._grade = card.grade;
        this._haveCard = card.haveCount;
        this._needCard = needCard[this._grade];
        this._amplificationType = CardPropertyType[card.type][card.number];
        this._amplification = CardPropertyAmplification[card.type][this._amplificationType] * card.grade;
        this.propertyAmplification();
        this.updateCard();
        this._card = {
            type: this._type,
            number: this._number,
            grade: this._grade,
            haveCount: this._haveCard,
        }
    }

    //属性增幅
    propertyAmplification() {
        if (!this._isCard) return;
        GameManager.Instance.userData.propertyAmplification[this._amplificationType] += this._amplification;
    }

    //增幅详情
    getProperty(): string {
        return `${PROPERTY[this._amplificationType]} ${this._amplification}%`;
    }

    updateCard(card: Card = null) {
        if (card) {
            if (card.type != this._type || card.number != this._number) return;
            this._grade = card.grade;
            this._haveCard = card.haveCount;
            this._needCard = needCard[this._grade];
            this._card.grade = this._grade;
            this._card.haveCount = this._haveCard;
        }
        this.GradeLabel.string = String(this._grade);
        this.HaveLabel.string = String(this._haveCard);
        this.NeedLabel.string = String(this._needCard);
        let progress: number = this._haveCard / this._needCard;
        if (progress >= 1) {
            this.UpgradeNode.active = true;
            this.UpgradeAnimation.active = true;
        } else {
            this.UpgradeNode.active = false;
            this.UpgradeAnimation.active = false;
        }
        this.UpgradeProgress.progress = progress;
    }

    evolution(type: CARDTYPE, number: number) {
        if (type != this._type || number != this._number) return;
        let index = GameManager.Instance.userData.card.findIndex(card => card.type === this._type && card.number === this._number);
        if (index == -1) {
            console.error(`找不到进化的卡片`);
        }
        this._grade++;
        this._haveCard -= this._needCard;
        this._card.grade = this._grade;
        this._needCard = needCard[this._grade];
        this._card.haveCount = this._haveCard;
        this._amplification += CardPropertyAmplification[this._type][this._amplificationType];
        GameManager.Instance.userData.card[index].grade = this._grade;
        GameManager.Instance.userData.card[index].haveCount = this._haveCard;
        this.evolutionPropertyAmplification();
        this.updateCard();
    }

    //属性增幅
    evolutionPropertyAmplification() {
        if (!this._isCard) return;
        //不是卡片的不需要加属性 --- 卡牌详情弹窗的不需要加
        GameManager.Instance.userData.propertyAmplification[this._amplificationType] += CardPropertyAmplification[this._type][this._amplificationType];
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.CARD_STATE, this.updateCard, this);
        EventManager.on(MyEvent.CARD_EVOLUTION, this.evolution, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.CARD_STATE, this.updateCard, this);
        EventManager.off(MyEvent.CARD_EVOLUTION, this.evolution, this);
    }

}


